﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using ECare.Client.templates;
using ECare.BUS.Data;
using ECare.BUS;

namespace ECare.Client.frms
{
    public partial class frmPlanDetails : frmInfo
    {
        public frmPlanDetails()
        {
            InitializeComponent();

            this.Load += frmPlanDetails_Load;

            this.grdStatus.CellContentClick += grdStatus_CellContentClick;
            this.grdStatus.CellFormatting += grdStatus_CellFormatting;
        }

        void grdStatus_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < grdStatus.Rows.Count)
            {
                if (grdStatus.Columns[e.ColumnIndex].Name == "plan_detail_status")
                {
                    var buttonCell = (DataGridViewButtonCell)grdStatus.Rows[e.RowIndex].Cells["btnSuspend"];

                    var currentstatus = grdStatus.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    if (currentstatus == PlanStatus.done.ToString())
                    {
                        this.grdStatus.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (currentstatus == PlanStatus.deleted.ToString())
                    {
                        this.grdStatus.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                        buttonCell.Value = "恢复(resume)";
                    }
                    else if (currentstatus == PlanStatus.working.ToString())
                    {
                        this.grdStatus.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
                        buttonCell.Value = "暂停(suspend)";
                    }
                    else if (currentstatus == PlanStatus.pending.ToString())
                    {buttonCell.Value = "删除(delete)";
                    }
                    else if (currentstatus == PlanStatus.pause.ToString())
                    {
                        this.grdStatus.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.SteelBlue;
                        buttonCell.Value = "恢复(resume)";
                    }
                    else
                    {

                    }

                }
            }
        }

        void grdStatus_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                var currentdetail = this.grdStatus.Rows[e.RowIndex].DataBoundItem as plan_detail_info;
                if (currentdetail.plan_detail_status == PlanStatus.done)
                {
                    MessageBox.Show("当前配料已经完成.", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (currentdetail.plan_detail_status == PlanStatus.working || currentdetail.plan_detail_status == PlanStatus.deleted || currentdetail.plan_detail_status == PlanStatus.pause || currentdetail.plan_detail_status==PlanStatus.suspend)
                {
                    if (MessageBox.Show("确实重启当前配料向导?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                        return;

                    var res = PlanHelper.CancelPlanDetail(currentdetail.plan_detail_id, PlanStatus.pending);
                    if (res)
                    {
                        currentdetail.plan_detail_status = PlanStatus.pending;
                        this.grdStatus.Refresh();
                    }

                }
                else if (currentdetail.plan_detail_status == PlanStatus.pending)
                {
                    if (MessageBox.Show("确实删除当前配料向导?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                        return;


                    var res = PlanHelper.CancelPlanDetail(currentdetail.plan_detail_id, PlanStatus.deleted);
                    if (res)
                    {
                        currentdetail.plan_detail_status = PlanStatus.deleted;
                        this.grdStatus.Refresh();
                    }
                }
                else{
                    return;
                }




                 }
        }

         void frmPlanDetails_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            frmloading = new frmLoading();
        
            //frmloading.Close();

             var worker = new CommonBackgroundWorker();
             worker.m_delegateFunction = new DelegateFunction(
                 () => PlanHelper.GetPlanDetails(this.entity as PlanInfo));
             worker.RunWorkerCompleted += worker_RunWorkerCompleted;
             worker.RunWorkerAsync();
        }

         void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
         {frmloading.Close();
             if (e.Result != null)
             {
                 this.source = new BindingList<plan_detail_info>( e.Result as List<plan_detail_info>);
                 this.grdStatus.DataSource = this.source;
             }

         }

        public BindingList<plan_detail_info> source = new BindingList<plan_detail_info>();

        //private async Task<List<plan_detail_info>> GetDetailsAsync()
        //{
        //    var plan = this.entity as PlanInfo;
        //    return await Task.Run(() => PlanHelper.GetPlanDetails(plan));
        //}

    }
}
